﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cash_desk_model;
using cash_desk_model.src.model;

namespace cash_desk
{
    public partial class ProductsForm : Form
    {
        private List<Product> _products;

        public ProductsForm()
        {
            InitializeComponent();
            this.InitProductsGrid();
        }

        public void InitProductsGrid()
        {
            this.productsGrid.Rows.Clear();
            CashDeskService.SetupDBConn();
            CashDeskService service = new CashDeskService();
            this._products = service.GetProducts();
            CashDeskService.DestroyDBConn();

            for (int i = 0; i < this._products.Count; i++)
            {
                this.productsGrid.Rows.Add(
                    this._products[i].Code,
                    this._products[i].ProductName,
                    this._products[i].Type,
                    this._products[i].Price,
                    this._products[i].Description,
                    this._products[i].Quantity,
                    this._products[i].Provider);
            }
        }

        private void searchButtom_Click(object sender, EventArgs e)
        {


            if (_products != null)
            {
                productsGrid.Rows.Clear();
            }

            string Code = null;
            if (searchProductCodeTextBox.Text != "")
            {
                Code = searchProductCodeTextBox.Text;
            }

            string Name = null;
            if (searchProductNameTextBox.Text != "")
            {
                Name = searchProductNameTextBox.Text;
            }

            string Type = null;
            if (searchProductTypeTextBox.Text != "")
            {
                Type = searchProductTypeTextBox.Text;
            }

            string Description = null;
            if (searchProductDescriptionTextBox.Text != "")
            {
                Description = searchProductDescriptionTextBox.Text;
            }

            string Provider = null;
            if (searchProductProviderTextBox.Text != "")
            {
                Provider = searchProductProviderTextBox.Text;
            }

            CashDeskService.SetupDBConn();

            CashDeskService service = new CashDeskService();
            this._products = service.GetProductsForSearch(Name, Code, Type, Description, Provider);

            CashDeskService.DestroyDBConn();

            for (int i = 0; i < this._products.Count; i++)
            {
                this.productsGrid.Rows.Add(

                    this._products[i].Code,
                    this._products[i].ProductName,
                    this._products[i].Type,
                    this._products[i].Price,
                    this._products[i].Description,
                    this._products[i].Quantity,
                    this._products[i].Provider);
            }
        }

        private void addProductButton_Click(object sender, EventArgs e)
        {
            CashDeskService.SetupDBConn();

            CashDeskService service = new CashDeskService();

            service.SaveNewProduct(this.productNameTextBox.Text, this.productTypeTextBox.Text,
                Double.Parse(this.productPriceTextBox.Text), this.productDescriptionTextBox.Text,
                Double.Parse(this.productQuantityTextBox.Text), this.productProviderTextBox.Text);

            CashDeskService.DestroyDBConn();

            this.InitProductsGrid();

            this.commentLabel.Visible = true;

            // Clean the form 
            this.productNameTextBox.Text = "";
            this.productTypeTextBox.Text = "";
            this.productPriceTextBox.Text = "";
            this.productDescriptionTextBox.Text = "";
            this.productQuantityTextBox.Text = "";
            this.productProviderTextBox.Text = "";
        }
    }
}
